package impl

import (
	"database/sql"

	"github.com/infraboard/mcube/app"
	"github.com/infraboard/mcube/logger"
	"github.com/infraboard/mcube/logger/zap"
	"google.golang.org/grpc"

	"gitee.com/yangdechao719/cmdb-g7/apps/resource"
	"gitee.com/yangdechao719/cmdb-g7/conf"
)

var (
	// Service 服务实例
	svr = &service{}
)

type service struct {
	db  *sql.DB
	log logger.Logger // 记录日志

	resource.UnimplementedServiceServer // grpc服务实现，嵌入grpc的UnimplementedServiceServer
}

func (s *service) Config() error { // grpc服务注册对象的配置方法实现
	db, err := conf.C().MySQL.GetDB()
	if err != nil {
		return err
	}

	s.log = zap.L().Named(s.Name())
	s.db = db
	return nil
}

func (s *service) Name() string { // grpc服务注册对象的名称
	return resource.AppName
}

func (s *service) Registry(server *grpc.Server) { // grpc服务的注册方法
	resource.RegisterServiceServer(server, svr) // 注册给grpc服务，给内部调用
}

func init() {
	app.RegistryGrpcApp(svr) //grpc服务注册对象
}
